<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**请求模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class StudentModel extends Model {

	public function register($uid,$openId,$name,$nickname,$studentId,$latitude,$longitude,$schooId='10183',$campus,$dormitory,$dorm,$profession,$sex,$desc='')
	{
     $data['studentId']=$studentId;
     $data['name']=$name;
     $data['nickname']=$nickname;
     $data['Uid']=$uid;
     $data['openId']=$openId;
     $data['schooId']=$schooId;
     $data['collegeId']=mb_substr($studentId,0,2);
     $data['majorId']=mb_substr($studentId,0,4);
     $data['ClassId']=mb_substr($studentId,0,6);
     $data['desc']=$desc;
     $data['latitude']=$latitude;
     $data['longitude']=$longitude;
     $data['campus']=$campus;
     $data['dormitory']=$dormitory;
     $data['dorm']=$dorm;
     $data['profession']=$profession;
     $data['sex']=$sex;
     $data['is_sign']=substr($studentId,2,2)==16?1:2;


     return $this->add($data);

	}
	public function is_verific($uid)
	{
	 return $this->where(array('Uid'=>$uid))->Field('studentId,verificFlag')->find();

	}
	public function queryStudent($uid)
	{

	 return $this->where(array('Uid'=>$uid,'verificFlag'=>1))->field('Uid,studentId,name,nickname,openId,verificFlag,schooId,collegeId,majorId,ClassId,teacherUid,latitude,longitude,campus,dormitory,dorm,is_sign,sex,profession')->find();


	}

    public function verifyOper($uid,$type,$verificUid,$teacherUid)
    {
     $where = array('Uid'=>$uid);
     $data = array('verificFlag'=>$type,'verificTime'=>getDateTime(),'verificUid'=>$verificUid,'teacherUid'=>$teacherUid);

    $res = $this->where($where)->save($data);

    if($type==1)
    {
      M('member')->where(array('uid'=>$uid,'status'=>1))->save(array('usertype'=>1));
    }

   return $res;

    }
    public function queryClassPersonnel($classid,$schooId='10183',$type)
    {
      if(empty($type))
      {
      $where = array('ClassId'=>$classid,'verificFlag'=>array('NEQ',2),'schooId'=>$schooId);
      }else
      {
      $where = array('ClassId'=>$classid,'verificFlag'=>$type,'schooId'=>$schooId);
      }

      $res = $this->where($where)->field('studentId,verificFlag,name,Uid,latitude,longitude,ClassId,schooId,substr(studentId,7,2) as sId,sex')->order('studentId')->select();

      return $res;


    }

    public function verifyTeacherAuth($studentId,$TeacherId)
    {
      $student = new \ Think \ Model();
     $res =  $student->query("select s.uid,s.verificFlag,s.studentId,s.name,s.sex,a.orgaid,a.organame,a.teacherId,a.leaveApprovalUid from ocenter_student s,ocenter_orga a where s.studentId = $studentId  and s.ClassId =
      a.orgaId and a.orgaType =3 ")[0];

      if($res['teacherId']==$TeacherId||$res['leaveApprovalUid']==$TeacherId)
      {
      return true;
      }else
      {
      return false;
      }


    }

    public function get_ClassTeacherInfo($studentId)
    {
      $student = new \ Think \ Model();

     return $student->query("select s.studentId,s.name,a.orgaid,organame,t.teacherUid,t.teacherName,s.sex from ocenter_student s,ocenter_orga a ,ocenter_teacher t where s.Uid = $studentId and s.ClassId = a.orgaId and a.orgaType =3 and a.teacherId = t.teacherUid")[0];
   // return $student->query("select s.studentId,s.name,a.orgaid,organame,t.teacherUid,t.teacherName from ocenter_student s,ocenter_orga a ,ocenter_teacher t where s.Uid = $studentId and s.ClassId = a.orgaId and a.orgaType =3 and a.teacherId = t.teacherUid")[0];

    }

    public function verifyRecord($studentUid,$studentId,$teacherUid,$verify,$status,$msg)
    {
    $data['teacherUid']=$teacherUid;
    $data['studentUid']=$studentUid;
    $data['studentId']=$studentId;
    $data['verifyFlg']=$verify;
    $data['status']=$status;
    $data['msg']=$msg;
    M('verifyrecord')->add($data);





    }

    public function is_ExistSid($studentId,$schooId)
    {
     return $this->where(array('studentId'=>$studentId,'verificFlag'=>1,'schooId'=>$schooId))->find();

    }

    public function updateDormLocation($Uid,$longitude,$latitude)
    {
    $data = array('longitude'=>$longitude,'latitude'=>$latitude);
    $this->where(array('Uid'=>$Uid,'verificFlag'=>1))->save($data);
    return true;

    }

    public function verifyTeacherClassAuth($teacherId,$classId,$schooId='10183')
    {
    $class = D('Orga')->get_OrgaInfo($classId,3,$schooId);
    if($class['teacherId']==$teacherId||$class['leaveApprovalUid']==$teacherId)
    {
    return true;
    }else
    {
    return false;
    }

    }

    public function classBatchVerific($teacherId,$classId,$Flg,$schooId='10183')
    {
     $where['ClassId']=$classId;
     $where['verificFlag']=	0;
     $where['schooId']=$schooId;
     $data['verificTime']=date('Y-m-d H:i:s');
     $data['verificUid']=$teacherId;
     $data['verificFlag']=$Flg;
    return $this->where($where)->save($data);

    }
    public function removeOperate($operateUid,$studentUid,$operateType=1)
    {
  // $res =  $this->where(array('studentId'=>$studentUid,'verificFlag'=>1))->save(array('verificFlag'=>3));
   $res =  $this->where(array('studentId'=>$studentUid))->delete();
   if($res)
   {
   	$removeRecord = M('removerecord');
    $data['studentUid']=$studentUid;
    $data['operateUid']=$operateUid;
    $data['operateTime']=time();
    $data['operateFlg']=1;
    $data['operateType']=$operateType;
    $data['desc']=$desc;
    $removeRecord->add($data);
   return true;
     }
    }

   public function is_Student($uid)
	{

	 return $this->where(array('Uid'=>$uid))->field('studentId,name,nickname,openId,verificFlag,schooId,collegeId,majorId,ClassId,teacherUid,latitude,longitude,is_sign,sex,campus,dormitory,dorm,profession')->find();


	}

   public function get_ClassRecord($classId,$schooId)
   {
   $student = new \ Think \ Model();
   return $this->query("select ifnull(sum(if(verificFlag=1,1,0)),0) as peopleNum ,ifnull(sum(if(verificFlag=0,1,0)),0) pendingNum from ocenter_student where ClassId = '$classId' and schooId ='$schooId' limit 1")[0];

   }
   public function updateStudentInfo($studentId,$data)
   {
    $where = array('studentId'=>$studentId,'verificFlag'=>1);
    return $this->where($where)->save($data);

   }












}